
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../common/style/extension_util.dart';
import '../style/app_style.dart';

enum AppEmptyType { notData, netAnomaly, loadFailed }

class AppEmptyView extends StatelessWidget {
  const AppEmptyView({
    Key? key,
    this.padding,
    this.emptyTips,
    this.emptyType = AppEmptyType.notData,
    this.emptyImage,
    this.actionText,
    this.actionCustom,
    this.onTapAction,
  }) : super(key: key);

  final AppEmptyType emptyType;
  final Widget? emptyImage;

  /// 默认值 notData：无数据 netAnomaly：网络异常 loadFailed：加载失败
  final String? emptyTips;

  /// 内边距，默认为null
  final EdgeInsetsGeometry? padding;

  /// 按钮文字，null不显示按钮
  final String? actionText;
  final Function()? onTapAction;

  /// 自定义的按钮，可以为任意的widget，而且点击事件完全由外部实现
  final Widget? actionCustom;

  _getText() {
    String _text = 'no_data'.tr;
    if (emptyType == AppEmptyType.loadFailed) {
      _text = 'load_error'.tr;
    } else if (emptyType == AppEmptyType.netAnomaly) {
      _text = 'server_error'.tr;
    }
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 40),
      child: Text(
        emptyTips ?? _text,
        textAlign: TextAlign.center,
        maxLines: 2,
        style: TextStyle(fontSize: 12, color: AppColor.textColor1, overflow: TextOverflow.ellipsis),
      ),
    );
  }

  _getImage() {
    if (emptyType == AppEmptyType.loadFailed) {
      return Image.asset(
        'img_empty.png'.toImgPath(),
        width: 100,
        height: 100,
      );
    } else if (emptyType == AppEmptyType.netAnomaly) {
      return Image.asset(
        'img_empty.png'.toImgPath(),
        width: 220,
        height: 167,
      );
    }
    return Image.asset(
      'img_empty'.toImgPath(),
      width: 220,
      height: 156,
    );
  }

  _getButton() {
    if (actionCustom != null) {
      return actionCustom;
    }
    return actionText == null
        ? Container()
        : ElevatedButtonWidget(onPressed: (){
      onTapAction?.call();
    }, child: Text(actionText! ,style: TextStyle(fontSize: 12,color: AppColor.textColor1),));
  }

  @override
  Widget build(BuildContext context) {
    Widget image;
    if (emptyImage != null) {
      image = SizedBox(width: 220, child: emptyImage);
    } else {
      image = _getImage();
    }
    return
    Container(
      color: Colors.transparent,
      padding: padding,
      child: Stack(children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: [
            image,
            Visibility(
              visible: emptyTips != '',
              child: Column(
                children: [
                  const SizedBox(height: 10),
                  _getText(),
                ],
              ),
            ),
            const SizedBox(height: 10),
            _getButton(),
          ],
        ),
        Positioned.fill(child: Container(color: Colors.transparent).addSingleChildScrollView())
      ],),
    );
  }
}
